using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms;
using System.Drawing;

namespace Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control
{
    /// <summary>
    /// A container button class associated with a certain message
    /// </summary>
    public class AEUCSettingButton : Button, IAEUCSerialization
    {
        /// <summary>
        /// Index of the associated message in the corresponding message array
        /// </summary>
        public int associate_message_reference;
        GenMSG AssociatedMessage;
        AEUCTabbedParentControl prntAssociate;

        #region Constructors
        /// <summary>
        /// Constructor creating the button container with no associated message
        /// </summary>
        /// <param name="iprntAssociate">Reference to the parent tab control</param>
        public AEUCSettingButton(AEUCTabbedParentControl iprntAssociate)
            : base()
        {
            prntAssociate = iprntAssociate;

            #region Context menu setup
            EventHandler eh = new EventHandler(ContextMenu);
            ContextMenuStrip =  new ContextMenuStrip();
            ContextMenuStrip.Items.Add("Edit", null, eh);
            ContextMenuStrip.Items.Add("Move", null, eh);
            #endregion

            Text = "Custom button";
        }
        /// <summary>
        /// Constructor creating the button container associating specified message to it
        /// </summary>
        /// <param name="iprntAssociate">Reference to the parent tab control</param>
        /// <param name="iAssociatedMessage">Reference to a specific message</param>
        public AEUCSettingButton(AEUCTabbedParentControl iprntAssociate, GenMSG iAssociatedMessage)
            : base()
        {
            prntAssociate = iprntAssociate;
            AssociatedMessage = iAssociatedMessage;

            #region Context menu setup
            EventHandler eh = new EventHandler(ContextMenu);
            ContextMenuStrip =  new ContextMenuStrip();
            ContextMenuStrip.Items.Add("Edit", null, eh);
            ContextMenuStrip.Items.Add("Move", null, eh);
            #endregion

            Text = "Custom button";
        }
        #endregion

        #region Context menu handlers
        /// <summary>
        /// Context menu function
        /// </summary>
        new void ContextMenu(object sender, EventArgs ea)
        {
            ToolStripItem tmpItem = (ToolStripItem)sender;
            if (tmpItem.Text == "Edit")
            {
                AEUCButtonEditForm abef = new AEUCButtonEditForm(this);
                abef.ShowDialog();
            }
            else if (tmpItem.Text == "Move")
            {
                Parent.MouseMove += new MouseEventHandler(Parent_MouseMove);
                tmpItem.Text = "Lock";
            }
            else if (tmpItem.Text == "Lock")
            {
                Parent.MouseMove -= new MouseEventHandler(Parent_MouseMove);
                tmpItem.Text = "Move";
            }
        }
        #endregion

        #region Overrided events for control functionality
        /// <summary>
        /// When the button is clicked initiates the associated message construction and sends it
        /// </summary>
        /// <param name="e">Button event arguments</param>
        protected override void OnClick(EventArgs e)
        {
            if (!prntAssociate.removing_aeuc_setting)
            {
                if (AssociatedMessage != null)
                {
                    byte[] msgTransmit = AssociatedMessage.GenerateMessage(null);
                    if (prntAssociate.sendMsg != null)
                        prntAssociate.sendMsg(msgTransmit);
                }
            }
        }
        /// <summary>
        /// Capture the parent's mouse move event's to change the controls location
        /// </summary>
        /// <param name="sender">Message sender</param>
        /// <param name="e">Mouse event arguments</param>
        void Parent_MouseMove(object sender, MouseEventArgs e)
        {
            Location = Parent.PointToClient(MousePosition);
        }
        /// <summary>
        /// ToString returns display string of the control
        /// </summary>
        /// <returns>Display string of the control</returns>
        public override string ToString()
        {
            return "Button to " + AssociatedMessage.ToString();
        }
        #endregion

        /// <summary>
        /// Public get/set accessor of the button associated message
        /// </summary>
        public GenMSG SetAssociatedMessage
        {
            get
            {
                return AssociatedMessage;
            }
            set
            {
                AssociatedMessage = value;
            }
        }
        /// <summary>
        /// Public associated parent accessor
        /// </summary>
        public AEUCTabbedParentControl GetAssociatedParent
        {
            get
            {
                return prntAssociate;
            }
        }
        /// <summary>
        /// Public get/set accessor for the button text display
        /// </summary>
        public string SetButtonName
        {
            get
            {
                return Text;
            }
            set
            { 
                Text = value;
                Graphics grphx = CreateGraphics();
                SizeF strTestSize = grphx.MeasureString(Text, Font);
                Size = new Size(20 + (int)strTestSize.Width, 10 + (int)strTestSize.Height);
            }
        }
        /// <summary>
        /// Resets the button's reference to the associated message
        /// </summary>
        public void ResetMessageReference(CommonMSGControl cmcDefinitios)
        {
            AssociatedMessage = cmcDefinitios.AEUC_MSG[associate_message_reference];
        }

        #region IAEUCSerialization Members
        /// <summary>
        /// Current element serialization state version
        /// </summary>
        public int serVersion
        {
            get
            {
                return 0x000;
            }
        }
        /// <summary>
        /// Saves this objec instance to a file
        /// </summary>
        /// <param name="srWriter">Binray writer of the associated file</param>
        public void Serialize(System.IO.BinaryWriter srWriter)
        {
            int message_reference = -1;
            #region Save the referencing number of the associated message
            bool found = false;
            for (int i = 0; i < prntAssociate.cmcDefinitios.AEUC_MSG.Count; i++)
            {
                if (prntAssociate.cmcDefinitios.AEUC_MSG[i] == AssociatedMessage)
                {
                    message_reference = i;
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                for (int i = 0; i < prntAssociate.cmcDefinitios.SPECMSG_MSG.Count; i++)
                {
                    if (prntAssociate.cmcDefinitios.SPECMSG_MSG[i] == AssociatedMessage)
                    {
                        message_reference = i;
                        found = true;
                        break;
                    }
                }
            }
            if (!found)
            {
                message_reference = -1;
            }
            #endregion
            AEUCSerializableButton serialButton = new AEUCSerializableButton(message_reference, Text, Location);
            serialButton.Serialize(srWriter);
        }
        /// <summary>
        /// Loads this objects instance from a file
        /// </summary>
        /// <param name="srReader">Binray reader of the associated file</param>
        /// <returns>Boolean representing the success of the operation</returns>
        public bool Deserialize(System.IO.BinaryReader srReader)
        {
            bool NoError = true;
            try
            {
                AEUCSerializableButton serialButton = new AEUCSerializableButton();
                NoError = serialButton.Deserialize(srReader);
                if (NoError)
                {
                    string btnName;
                    Point btnLoc;
                    serialButton.GetButtonInformation(out associate_message_reference, out btnName, out btnLoc);
                    SetButtonName = btnName;
                    Location = btnLoc;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                NoError = false;
            }
            return NoError;
        }
        #endregion
    }
}
